<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/8/31 17:04
 */

namespace module\user\model;

use mof\Model;
use think\model\relation\MorphTo;

class Favorite extends Model
{
    protected $name = 'user_favorite';

    protected $updateTime = false;

    protected array $searchFields = [
        'id'          => 'integer',
        'user_id'     => 'integer',
        'source_type' => 'string',
        'source_id'   => 'integer',
    ];

    /**
     * 模型关联
     * @var array
     */
    protected static array $morphInfo = [];

    public static function registerMorph(string|array $name, $class = null): void
    {
        if (is_array($name)) {
            foreach ($name as $key => $val) {
                static::$morphInfo[$key] = $val;
            }
        } elseif (empty($class)) {
            throw new \InvalidArgumentException('Invalid morph class');
        } else {
            static::$morphInfo[$name] = $class;
        }
    }

    public function source(): MorphTo
    {
        return $this->morphTo('source', static::$morphInfo);
    }
}